package com.itheima.userprofile.service.impl;

import com.itheima.userprofile.bean.dto.ModelDto;
import com.itheima.userprofile.bean.dto.TagDto;
import com.itheima.userprofile.bean.dto.TagModel;
import com.itheima.userprofile.bean.po.ModelPo;
import com.itheima.userprofile.bean.po.TagPo;
import com.itheima.userprofile.bean.repo.ModelRepository;
import com.itheima.userprofile.bean.repo.TagRepository;
import com.itheima.userprofile.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagRepository tagRepository;

    @Autowired
    private ModelRepository modelRepository;

    @Override
    public void saveAll(List<TagDto> list) {
        //将123级标签数据保存起来.
        List<TagPo> poList = list.stream().map(this::dto2Po).collect(Collectors.toList());
//        TagPo tag1 = poList.get(0);
//        TagPo tag2 = poList.get(1);
//        TagPo tag3 = poList.get(2);

        /*tag1.setPid(-1L);
        //先判断有没有这个1级的标签, 同级别,同名字,同父ID
        *//*金融
                人口属性
                人口属性
        电商
                人口属性*//*

        //看有没有重复
        TagPo tmpTag = tagRepository.findByLevelAndNameAndPid(tag1.getLevel(), tag1.getName(), tag1.getPid());
        if (tmpTag == null){
            tmpTag = tagRepository.save(tag1);
        }
        tag2.setPid(tmpTag.getId());

        tmpTag = tagRepository.findByLevelAndNameAndPid(tag2.getLevel(), tag2.getName(), tag2.getPid());
        if (tmpTag == null){
            tmpTag = tagRepository.save(tag2);
        }

        tag3.setPid(tmpTag.getId());
        //保存3级.
        tmpTag = tagRepository.findByLevelAndNameAndPid(tag3.getLevel(), tag3.getName(), tag3.getPid());
        if (tmpTag == null){
            //没找到,那么就将这个临时的tag进行赋值
            tmpTag = tagRepository.save(tag3);
        }*/

        TagPo tmpTag = new TagPo();
        tmpTag.setId(-1L);
        for (int i = 0; i < 3; i++) {
            TagPo tag = poList.get(i);
            tag.setPid(tmpTag.getId());
            tmpTag = tagRepository.findByLevelAndNameAndPid(tag.getLevel(), tag.getName(), tag.getPid());
            if (tmpTag == null) {
                tmpTag = tagRepository.save(tag);
            }
        }
    }

    @Override
    public List<TagDto> findTagsByPid(Long pid) {
        List<TagPo> poList = tagRepository.findByPid(pid);
        return poList.stream().map(this::po2Dto).collect(Collectors.toList());
    }

    @Override
    public void saveTagModel(TagDto tag, ModelDto model) {
        //1.先保存4级标签
        TagPo tagPo = tagRepository.save(dto2Po(tag));
        //2.根据4级标签插入模型
        modelRepository.save(dto2Po(model, tagPo.getId()));
    }

    @Override
    public List<TagModel> findAllTagModel(Long pid) {
        ArrayList<TagModel> list = new ArrayList<>();
        //根据3级pid查询4级列表
        List<TagPo> tagList = tagRepository.findByPid(pid);
        for (TagPo tagPo : tagList) {
            ModelPo modelPo = null;
            if (tagPo.getLevel() == 4) {
                Long tagId = tagPo.getId();
                modelPo = modelRepository.findByTagId(tagId);
                list.add(new TagModel(po2Dto(tagPo), po2Dto(modelPo)));
            } else {
                list.add(new TagModel(po2Dto(tagPo), null));
            }
        }
        return list;
    }

    @Override
    public void saveTag(TagDto dto) {
        tagRepository.save(dto2Po(dto));
    }

    private ModelDto po2Dto(ModelPo modelPo) {
        ModelDto modelDto = new ModelDto();
        modelDto.setId(modelPo.getId());
        modelDto.setName(modelPo.getName());
        modelDto.setMainClass(modelPo.getMainClass());
        modelDto.setPath(modelPo.getPath());
        modelDto.setArgs(modelPo.getArgs());
        modelDto.setState(modelPo.getState());
        modelDto.setSchedule(modelDto.parseDate(modelPo.getSchedule()));
        return modelDto;
    }

    //modelDto => ModelPo
    private ModelPo dto2Po(ModelDto modelDto, Long id) {
        ModelPo modelPo = new ModelPo();
        modelPo.setId(modelDto.getId());
        //设置4级ID为外部传入的ID
        modelPo.setTagId(id);
        modelPo.setName(modelDto.getName());
        modelPo.setMainClass(modelDto.getMainClass());
        modelPo.setPath(modelDto.getPath());
        modelPo.setSchedule(modelDto.getSchedule().toString());
        modelPo.setCtime(new Date());
        modelPo.setUtime(new Date());
        modelPo.setState(modelDto.getState());
        modelPo.setArgs(modelDto.getArgs());
        return modelPo;
    }

    //TagPo => TagDto
    private TagDto po2Dto(TagPo po) {
        return new TagDto(po.getId(), po.getName(), po.getRule(), po.getLevel(), po.getPid(),
                po.getState(), po.getBusiness());
    }

    //TagDto => TagPo
    private TagPo dto2Po(TagDto dto) {
        TagPo po = new TagPo();
        po.setId(dto.getId());
        po.setName(dto.getName());
        po.setRule(dto.getRule());
        po.setLevel(dto.getLevel());
        po.setPid(dto.getPid());
        po.setState(dto.getState());
        return po;
    }
}
